#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
收银台 - 交易撤销功能使用示例
"""

import os
import sys

# 添加项目根目录到Python路径，确保能够导入jlpay包
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from examples.base_example import BaseExample
from jlpay.services.cashier_service import CashierService
from jlpay.model.cashier import TradeCancelRequest


class TradeCancelExample(BaseExample):
    """交易撤销示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.cashier.trade_cancel")
        # 创建收银台服务
        self.cashier_service = CashierService(self.config)
    
    def build_trade_cancel_request(self):
        """构建交易撤销请求"""
        request = TradeCancelRequest()
        
        # 设置请求参数
        request.set_merch_no("849584358120018")
        request.set_out_trade_no("ld201911023465530")
        request.set_ori_out_trade_no("ld201911023465529")
        request.set_remark("撤销交易")
        
        return request
    
    def demo(self):
        """执行交易撤销示例"""
        self.logger.info("开始执行交易撤销示例")
        
        # 创建交易撤销请求
        request = self.build_trade_cancel_request()
        
        # 发送请求并处理响应
        response = self.send_request_and_handle_response(
            self.cashier_service.cancel_trade,
            "交易撤销",
            request
        )
        
        # 处理响应结果
        if response and response.is_success():
            self.logger.info(f"响应报文：{response.to_dict()}")
        
        self.logger.info("交易撤销示例执行完毕")


if __name__ == "__main__":
    logger = TradeCancelExample().logger
    logger.info("==================================================")
    logger.info("===== 交易撤销示例 开始 =====")
    logger.info("==================================================")
    
    example = TradeCancelExample()
    example.demo()
    
    logger.info("==================================================")
    logger.info("===== 交易撤销示例执行完毕 =====")
    logger.info("==================================================") 